package com.xijia.wuyouwallet.fragment;
import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.activity.AplipayBingdingActivity;
import com.xijia.wuyouwallet.activity.AttestationActivity;
import com.xijia.wuyouwallet.activity.AttestationSuccessActivity;
import com.xijia.wuyouwallet.activity.BankCardActivity;
import com.xijia.wuyouwallet.activity.CommonFwvActivity;
import com.xijia.wuyouwallet.activity.CommonWvActivity;
import com.xijia.wuyouwallet.activity.CrashActivity;
import com.xijia.wuyouwallet.activity.FeedbackActivity;
import com.xijia.wuyouwallet.activity.MeWalletActivity;
import com.xijia.wuyouwallet.activity.PersonInfoActivity;
import com.xijia.wuyouwallet.activity.RelieveAcountActivity;
import com.xijia.wuyouwallet.activity.ServeActivity;
import com.xijia.wuyouwallet.activity.SettingActivity;
import com.xijia.wuyouwallet.activity.TradeBillActivity;
import com.xijia.wuyouwallet.activity.UpgradeNewActivity;
import com.xijia.wuyouwallet.adapter.MeVpAdapter;
import com.xijia.wuyouwallet.base.BaseFragment;
import com.xijia.wuyouwallet.utils.Constants;
import com.xijia.wuyouwallet.utils.StringUtils;
import com.xijia.wuyouwallet.utils.UrlConfig;
import java.util.ArrayList;
import java.util.List;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/7/11 0011.
 */

public class MeFragment extends BaseFragment implements View.OnClickListener,BGARefreshLayout.BGARefreshLayoutDelegate,MeVpAdapter.onVpCallBack{

    private ImageView me_fragment_img,me_fragment_setting;
    private TextView me_fragment_phone;
    private TextView me_fragment_recommend;
    private TextView me_fragment_my;
    private LinearLayout me_fragment_designation_ll;

    private TextView me_fragment_upgrade,me_fragment_rank;

    private LinearLayout me_fragment_agency,me_fragment_serve,me_fragment_suggestion,me_fragment_freshman
            ,me_fragment_wallet,me_fragment_profit,me_fragment_authentication,me_fragment_bank,me_fragment_aipay;


    private BGARefreshLayout me_fragment_refresh;

    private ViewPager me_fragment_vp;
    private RadioGroup me_fragment_rg;
    private MeVpAdapter meVpAdapter;
    private List<Integer> blist = new ArrayList<Integer>();


    private AlertDialog operationDialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me,null);
        initView(view);
        return view;
    }
    private void initView(View view){
        me_fragment_img = (ImageView) view.findViewById(R.id.me_fragment_img);
        me_fragment_img.setOnClickListener(this);
        me_fragment_setting = (ImageView) view.findViewById(R.id.me_fragment_setting);
        me_fragment_setting.setOnClickListener(this);
        me_fragment_my = (TextView) view.findViewById(R.id.me_fragment_my);
        me_fragment_my.setOnClickListener(this);
        me_fragment_phone = (TextView) view.findViewById(R.id.me_fragment_phone);
        me_fragment_recommend = (TextView) view.findViewById(R.id.me_fragment_recommend);
        me_fragment_agency = (LinearLayout) view.findViewById(R.id.me_fragment_agency);
        me_fragment_agency.setOnClickListener(this);
        me_fragment_serve = (LinearLayout) view.findViewById(R.id.me_fragment_serve);
        me_fragment_serve.setOnClickListener(this);
        me_fragment_suggestion = (LinearLayout) view.findViewById(R.id.me_fragment_suggestion);
        me_fragment_suggestion.setOnClickListener(this);
        me_fragment_freshman = (LinearLayout) view.findViewById(R.id.me_fragment_freshman);
        me_fragment_freshman.setOnClickListener(this);
        me_fragment_upgrade = (TextView) view.findViewById(R.id.me_fragment_upgrade);
        me_fragment_upgrade.setOnClickListener(this);
        me_fragment_wallet = (LinearLayout) view.findViewById(R.id.me_fragment_wallet);
        me_fragment_wallet.setOnClickListener(this);
        me_fragment_profit = (LinearLayout) view.findViewById(R.id.me_fragment_profit);
        me_fragment_profit.setOnClickListener(this);
        me_fragment_authentication = (LinearLayout) view.findViewById(R.id.me_fragment_authentication);
        me_fragment_authentication.setOnClickListener(this);
        me_fragment_bank = (LinearLayout) view.findViewById(R.id.me_fragment_bank);
        me_fragment_bank.setOnClickListener(this);
        me_fragment_aipay = (LinearLayout) view.findViewById(R.id.me_fragment_aipay);
        me_fragment_aipay.setOnClickListener(this);
        me_fragment_designation_ll = (LinearLayout) view.findViewById(R.id.me_fragment_designation_ll);
        me_fragment_refresh = (BGARefreshLayout) view.findViewById(R.id.me_fragment_refresh);
        me_fragment_refresh.setDelegate(this);
        me_fragment_refresh.setIsShowLoadingMoreView(false);
        me_fragment_rank = (TextView) view.findViewById(R.id.me_fragment_rank);
        me_fragment_refresh.setRefreshViewHolder(new BGANormalRefreshViewHolder(MyApp.getInstance(), true));
        me_fragment_vp = (ViewPager) view.findViewById(R.id.me_fragment_vp);
        me_fragment_rg = (RadioGroup) view.findViewById(R.id.me_fragment_rg);
        initData();
        initBanner();
    }

    private void initBanner(){
        blist.add(R.layout.list_me_vp);
        blist.add(R.layout.list_me_vp2);
        meVpAdapter = new MeVpAdapter(getActivity(), blist,MeFragment.this);
        me_fragment_vp.setAdapter(meVpAdapter);
        if (blist.size()>0) {
            for (int i = 0; i < blist.size(); i++) {
                LayoutInflater.from(getActivity()).inflate(R.layout.point_layout,
                        me_fragment_rg);
            }
        }
        me_fragment_rg.check(me_fragment_rg.getChildAt(0).getId());
        me_fragment_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                me_fragment_rg.check(me_fragment_rg.getChildAt(arg0).getId());
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
    private void initData(){

        //判断是否实名认证
        if (MyApp.userData.getData().getCertification_state().equals("0")){
            //手机号
            me_fragment_phone.setText(StringUtils.hintPhone(MyApp.userData.getData().getPhone()));
            me_fragment_designation_ll.setVisibility(View.VISIBLE);
        }else {
            me_fragment_phone.setText(MyApp.userData.getData().getNick_name());
            me_fragment_designation_ll.setVisibility(View.GONE);
        }
        //判断是否是会员
       /* if (MyApp.userData.getData().getRole_id().equals("1")){
            me_fragment_designation_img.setImageResource(R.mipmap.ic_uc_grade_lv3);
        }else if (MyApp.userData.getData().getRole_id().equals("2")){
            me_fragment_designation_img.setImageResource(R.mipmap.ic_uc_grade_lv2);
        }else if (MyApp.userData.getData().getRole_id().equals("3")){
            me_fragment_designation_img.setImageResource(R.mipmap.ic_uc_grade_lv1);
        }*/
        //设置头像
        if (MyApp.userData.getData().getAvata()!=null||!MyApp.userData.getData().getAvata().equals("")){
            Picasso.with(getActivity()).load(MyApp.userData.getData().getAvata()).fit().into(me_fragment_img);
        }
        //设置会员信息
        me_fragment_rank.setText(MyApp.userData.getData().getRole());
        //推荐人隐藏掉
        /*if (MyApp.userData.getData().getParent_tel()==null||MyApp.userData.getData().getParent_tel().equals("")){
            me_fragment_recommend.setText("我的推荐人是：无");
        }else {
            me_fragment_recommend.setText("我的推荐人是："+ StringUtils.hintPhone(MyApp.userData.getData().getParent_tel()));
        }*/

    }
    @Override
    protected void lazyLoad() {

    }
    @Override
    public void onClick(View view) {
        Bundle bundle = null;
        switch (view.getId()){
            case R.id.me_fragment_img:
                jumpToPage(PersonInfoActivity.class,true, Constants.REQUEST_DATA);
                break;
            case R.id.me_fragment_setting:
                jumpToPage(SettingActivity.class);
                break;
            case R.id.me_fragment_wallet://我的钱包
                if (MyApp.userData.getData().getCertification_state().equals("0")){
                    initOperationDialog(2);
                    return;
                }
                jumpToPage(MeWalletActivity.class);
                break;
            case R.id.me_fragment_profit://我的盈利
                bundle = new Bundle();
                bundle.putString("url", UrlConfig.ME_PROFIT+"/id/"+MyApp.userData.getData().getUser_id());
                bundle.putString("title","我的盈利");
                jumpToPage(CommonFwvActivity.class,bundle);
                break;
            case R.id.me_fragment_authentication://实名认证
                if (MyApp.userData.getData().getCertification_state().equals("0")){
                    jumpToPage(AttestationActivity.class);
                }else if (MyApp.userData.getData().getCertification_state().equals("1")){
                    jumpToPage(AttestationSuccessActivity.class);
                }
                break;
            case R.id.me_fragment_bank://银行卡
                if (MyApp.userData.getData().getCertification_state().equals("0")){
                    initOperationDialog(2);
                    return;
                }
                jumpToPage(BankCardActivity.class);
                break;
            case R.id.me_fragment_agency://我的团队
                bundle = new Bundle();
                bundle.putString("url", UrlConfig.ME_TEAM+"/id/"+MyApp.userData.getData().getUser_id());
                bundle.putString("title","我的团队");
                jumpToPage(CommonFwvActivity.class,bundle);
                break;
            case R.id.me_fragment_serve://联系客服
                jumpToPage(ServeActivity.class);
                break;
            case R.id.me_fragment_suggestion://意见反馈
                jumpToPage(FeedbackActivity.class);
                break;
            case R.id.me_fragment_freshman://新手指引
                bundle = new Bundle();
                bundle.putString("url", UrlConfig.COMMON_QUESTION_URL);
                bundle.putString("title","常见问题");
                jumpToPage(CommonWvActivity.class,bundle);
                break;
            case R.id.me_fragment_upgrade://我要升级
                if (MyApp.userData.getData().getCertification_state().equals("0")){
                    initOperationDialog(2);
                    return;
                }
                jumpToPage(UpgradeNewActivity.class);
                break;
            case R.id.me_fragment_my:
                jumpToPage(PersonInfoActivity.class,true, Constants.REQUEST_DATA);
                break;
            case R.id.me_fragment_aipay://支付宝
                if (MyApp.userData.getData().getAccount_info().getAlipay().getState()==0){
                    jumpToPage(AplipayBingdingActivity.class,true,Constants.REQUEST_DATA);
                }else {
                    bundle = new Bundle();
                    bundle.putString("type","alipay");
                    jumpToPage(RelieveAcountActivity.class,bundle,true,Constants.REQUEST_DATA,false);
                }
                break;
            /*case R.id.me_fragment_personal_ll:
                jumpToPage(PersonInfoActivity.class,true, Constants.REQUEST_DATA);
                break;*/
           /* case R.id.me_fragment_profitMoney_ll://累计分润
                *//*bundle = new Bundle();
                bundle.putString("url", UrlConfig.ADDUP_FENRUN_URL);
                bundle.putString("title","常见问题");
                jumpToPage(CommonWvActivity.class,bundle);*//*
                showToast("正在开发中，敬请期待");
                break;
            case R.id.me_fragment_commission_ll://累计返佣
                bundle = new Bundle();
                bundle.putString("url", UrlConfig.GET_COMMISSION_URL+"/token/"+MyApp.userData.getData().getToken()+"/id/"+MyApp.userData.getData().getUser_id());
                //bundle.putString("title","常见问题");
                jumpToPage(CommonWvActivity.class,bundle);
                break;*/
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyApp.isRefresh){
            initData();
            MyApp.isRefresh = false;
        }
    }

    /**
     * 对话框
     *
     * @param hitType
     */
    private void initOperationDialog(final int hitType) {
        operationDialog=new AlertDialog.Builder(getActivity()).create();
        //点击外部区域不能取消dialog
        operationDialog.setCanceledOnTouchOutside(false);
        //dialog.setOnKeyListener(keylistener);
        operationDialog.show();
        Window window = operationDialog.getWindow();
        window.setContentView(R.layout.dialog_operation);
        window.getDecorView().setBackgroundColor(Color.TRANSPARENT);
        //View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_operation, null);
        TextView dialog_binding_sure = (TextView) window.findViewById(R.id.dialog_binding_sure);
        TextView dialog_binding_cancle = (TextView) window.findViewById(R.id.dialog_binding_cancle);
        //TextView dialog_binding_title = (TextView) view.findViewById(R.id.dialog_binding_title);
        TextView dialog_binding_content = (TextView) window.findViewById(R.id.dialog_binding_content);
        if (hitType==1){
            dialog_binding_content.setText("您即将进入的模块为会员收费模块，需付费开通，如需开通请点击立即前往");
        }else if (hitType==2){
            dialog_binding_content.setText("您尚未实名认证");
        }
        dialog_binding_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operationDialog.dismiss();
            }
        });
        dialog_binding_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operationDialog.dismiss();
                if (hitType==1){
                    jumpToPage(UpgradeNewActivity.class);
                }else if (hitType==2){
                    jumpToPage(AttestationActivity.class);
                }
            }
        });
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        initData();
        me_fragment_refresh.endRefreshing();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

    @Override
    public void onVpClick(int page, int position) {
        if (page==0){
            if (position==0){//提现
                //showToast("开发中,敬请期待");
                if (MyApp.userData.getData().getCertification_state().equals("0")){
                    initOperationDialog(2);
                    return;
                }
                jumpToPage(CrashActivity.class);
            }else if (position==1){//签到
                if (MyApp.userData.getData().getRole_id().equals("3")){
                    showToast("请开通会员");
                    return;
                }
                showToast("功能正在开发中");
            }else if (position==2){//风险评测
                if (MyApp.userData.getData().getRole_id().equals("3")){
                    showToast("请开通会员");
                    return;
                }
                showToast("功能正在开发中");
            }else if (position==3){//银行卡
                //showToast("开发中,敬请期待");
                MyApp.isBankFinsh = false;
                jumpToPage(BankCardActivity.class);
            }
        }else if (page==1){
            if (position==0){//优惠券
                if (MyApp.userData.getData().getRole_id().equals("3")){
                    showToast("请开通会员");
                    return;
                }
                showToast("功能正在开发中");
            }else if (position==1){//充值
                if (MyApp.userData.getData().getRole_id().equals("3")){
                    showToast("请开通会员");
                    return;
                }
                showToast("功能正在开发中");
            }else if (position==2){//账单
                jumpToPage(TradeBillActivity.class);
            }else if (position==3){

            }
        }
    }
}
